package com.fwtai.swagger;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static springfox.documentation.builders.PathSelectors.regex;

/**
 * swagger2配置
 * @作者 田应平
 * @版本 v1.0
 * @创建时间 2020/3/23 18:00
 * @QQ号码 444141300
 * @Email service@yinlz.com
 * @官网 <url>http://www.fwtai.com</url>
*/
@Configuration
@EnableSwagger2//若不想在页面显示则移除此注解即可,当然也可以同时删除依赖 springfox-swagger-ui
public class Swagger2{

  //swagger2的配置文件,打开 http://127.0.0.1:801/swagger-ui.html
  @Bean
  public Docket docketV1() {
    return platformApi("v1.0.0");
  }

  public Docket platformApi(final String groupName) {
    return new Docket(DocumentationType.SWAGGER_2).enable(true).groupName(groupName).apiInfo(apiInfo()).forCodeGeneration(true)
      .select()
      .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
      .paths(regex("^.*(?<!error)$"))
      .build()//下面两行是token,若不用注释掉即可
      .securitySchemes(securitySchemes())
      .securityContexts(securityContexts());
  }

  private List<SecurityScheme> securitySchemes(){
    final List<SecurityScheme> apiKeyList = new ArrayList<>(2);
    apiKeyList.add(new ApiKey("accessToken","accessToken","header"));
    apiKeyList.add(new ApiKey("refreshToken","refreshToken","header"));
    return apiKeyList;
  }

  private List<SecurityContext> securityContexts(){
    final List<SecurityContext> securityContexts = new ArrayList<>(1);
    securityContexts.add(
      SecurityContext.builder()
        .securityReferences(defaultAuth())
        .forPaths(regex("^(?!auth).*$"))
        .build());
    return securityContexts;
  }

  private List<SecurityReference> defaultAuth(){
    final AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
    authorizationScopes[0] = new AuthorizationScope("global","accessEverything");;
    final List<SecurityReference> securityReferences = new ArrayList<>();
    securityReferences.add(new SecurityReference("accessToken", authorizationScopes));
    securityReferences.add(new SecurityReference("refreshToken", authorizationScopes));
    return securityReferences;
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder().title("api接口服务").description("<br />©2023 Copyright. <a href=\"http://tfkj.top\" target=\"_blank\">驼峰科技有限责任公司</a><br />文档说明:<br />返回json格式数据 {\"code\":200,\"msg\":\"操作成功\",\"total\" : 128,\"record\":10,\"data\":{}或[{}]},其中<br />code:200,时说明请求操作成功;<br />code:198,用于特殊情况处理;<br />code:201,表示暂无数据;<br />code:205,时表示token无效或过期重新需要登录;<br />code:401,表示表示没有操作权限;<br />total:是返回总条数总记录数;<br />record:是返回本次请求的条数;<br />msg:表示返回提示信息;<br />注意:total|record仅在返回列表时有,其它操作不一定存在;")
      .termsOfServiceUrl("http://www.fwtai.com")
      .contact(new Contact("http://www.fwt.cloud","http://www.fwt.cloud","444141300@qq.com")).license("保密版本")
      .licenseUrl("http://www.fwt.cloud").version("v1.0").build();
  }
}